﻿//创建时间: 2010-10-11
//创建作者: 李 城
//功能说明: 站点 添加 修改 
//最后修改: 2010-10-11

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TRPOP.Common;
using TRPOP.CMS.Web.UI;
using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using System.Collections;
using TRPOP.CMS.UserManage;

namespace TRPOP.CMS.Web.Admin
{
    public partial class UserBase_save : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.SystemManage_User;
        }
        #endregion

        #region 页面变量
        protected int m_id = 0;
        protected string m_title = "添加";
        protected AdminUserInfo mAdminUserBaseInfo = new AdminUserInfo();
        protected string strpwdInfo = "新增用户需指定密码，否则密码默认为123456";
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strMSG = "登录名 、用户昵称";
            if (!PageAssist.CheckForm(txtName, ref strMSG) ||
                !PageAssist.CheckForm(txtLogName, ref strMSG)
                )
            {
                Alert(strMSG);
                return;
            }
            int.TryParse(GetRequestViewState("id"), out m_id);
            mAdminUserBaseInfo = m_IAdminUser.getUserBaseInfo(m_id) as AdminUserInfo;
            if (mAdminUserBaseInfo == null)
            {
                mAdminUserBaseInfo = new AdminUserInfo();
                strMSG = "已存在[ {0} ]用户名";
                if (m_IAdminUser.GetUserListByCondition(0, " and LogName='" + txtLogName.Text.Trim().SQLFilter() + "' ", "").Count > 0)
                {
                    Alert(string.Format(strMSG, txtLogName.Text.Trim().SQLFilter()));
                    return;
                }
                mAdminUserBaseInfo.LastLogTime = DateTime.Now;
            }
            else
            {
                if (mAdminUserBaseInfo.LogName.Trim() != txtLogName.Text.Trim())
                {
                    strMSG = "已存在[ {0} ]用户名";
                    if (m_IAdminUser.GetUserListByCondition(0, " and LogName='" + txtLogName.Text.Trim().SQLFilter() + "' ", "").Count > 0)
                    {
                        Alert(string.Format(strMSG, txtLogName.Text.Trim().SQLFilter()));
                        return;
                    }
                }
            }
            strpwdInfo = "如果未输入，则不修改密码";
            mAdminUserBaseInfo.LogName = txtLogName.Text;
            mAdminUserBaseInfo.Name = txtName.Text;
            mAdminUserBaseInfo.Sex = rbtSex.SelectedIndex <= 0;
            mAdminUserBaseInfo.LogPWD = mAdminUserBaseInfo.LogPWD.IsEmpty() ?
                                    ("123456".MD5Normal()) :
                                    mAdminUserBaseInfo.LogPWD;
            if (txtPWD1.Text.Trim().IsEmpty() == false)
                mAdminUserBaseInfo.LogPWD = txtPWD1.Text.Trim().MD5Normal();
            if (m_id > 0)
            {
                base.SaveLog(Plugin.LogKeys.OperObject.AdminOper, Plugin.LogKeys.OperResult.EditSubmit, m_id.ToString());
            }
            else
            {
                base.SaveLog(Plugin.LogKeys.OperObject.AdminOper, Plugin.LogKeys.OperResult.AddSubmit, mAdminUserBaseInfo.UserBaseId.ToString());
            }
            if (m_IAdminUser.SaveUserInfo(mAdminUserBaseInfo))
            {
                SubmitAlertAndThisNewLocation(EnumSubmitResult.Success, "UserBase_list.aspx");
            }
            else
            {
                SubmitAlertAndThisNewLocation(EnumSubmitResult.Failed, "UserBase_list.aspx");
            }

        }
        #endregion

        #region 页面方法

        public void LoadBase()
        {
            int.TryParse(InitRequestViewState("id"), out m_id);
            if (m_id > 0)
            {
                mAdminUserBaseInfo = m_IAdminUser.getUserBaseInfo(m_id) as AdminUserInfo;
                if (mAdminUserBaseInfo != null)
                {
                    strpwdInfo = "如果未输入，则不修改密码";
                    InitPageViewState("model", mAdminUserBaseInfo);
                    m_title = "编辑";
                    txtLogName.Text = mAdminUserBaseInfo.LogName;
                    txtName.Text = mAdminUserBaseInfo.Name;
                    rbtSex.SelectedValue = mAdminUserBaseInfo.Sex.ToString().ToLower();
                }
            }
        }
        #endregion

    }
}
